package Class_test2;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;

public class MyReflect1 {
    public static void main(String[] args) throws IllegalAccessException, IOException {
        Student s=new Student("张三",18);
        saveObject(s);
    }
    public static void saveObject(Object obj) throws IllegalAccessException, IOException {
       //获取class字节码文件
        Class clazz=obj.getClass();

        BufferedWriter bw=new BufferedWriter(new FileWriter("a.txt"));
        //获取所有成员变量
        Field[] files=clazz.getDeclaredFields();
        for (Field file : files) {
            file.setAccessible(true);
            //获取成员变量的名称
            String name=file.getName();
            //获取成员变量的值
            Object value=file.get(obj);
            bw.write(name+"="+value);
            bw.newLine();
        }
        bw.close();
    }
}
